<?php
/**用于添加、编辑前置任务内容
 * @author wuguowen, Mar. 03 2005
 */
require_once("../../includes/config.inc.php");
require_once("$CFG_REAL_CLASSES/PreTaskCheck.class.php");

//定义工作任务表的变量
$iEPID      = (isset($_SESSION["cfg_login_EPID"])?$_SESSION["cfg_login_EPID"]:"-1");		//获取传递过来的企业ID
$iProjectID = (isset($_SESSION["wbs"]["pro_id"])?$_SESSION["wbs"]["pro_id"]:"-1");	         //获取传递过来的项目ID
$iTaskID    = fn_getParam("intTaskID",0);		//工作分解结构的记录ID
$sAction    = fn_getParam("strAction");			//动作，是添加记录还是修改记录
$iID        = fn_getParam("intID",0);			//前置任务记录的ID
		
$iPreTaskID = fn_getParam("strPreTaskID",0);
$sPreTaskType=fn_getParam("strPreTaskType","");
$sStayTime  = fn_getParam("strStayTime","");
$iOrderNumb = fn_getParam("strOrderNumb",-1);

$sAction = substr($sAction,0,8);
//echo $sAction.$iPreTaskID."\t".$sPreTaskType."\t".$sStayTime."\t".$iOrderNumb;

//检查是否有相同的前置任务存在
$sResult = "";
$sql = "Select Count(ID) From $TABLE_PRETASKLIST Where TaskID=$iTaskID and PreTaskID=$iPreTaskID";
$iCount = $DB->get_firstfield($sql);

if ($sAction==ACTION_INSERT."CC" and $iCount!=null and $iCount!="" and $iCount>0) {
	$sResult = "<font color='red'>".$messages["apps.wbs.preTaskAction.key004"]."</font>";
} else if ($sAction==ACTION_INSERT."CC" and ($iTaskID==0 or $iPreTaskID==0)) {
	$sResult = "<font color='red'>".$messages["apps.wbs.preTaskAction.key005"]."</font>";
} else if (($sAction==ACTION_UPDATE."CC") and $iID==0) {
	$sResult = "<font color='red'>".$messages["apps.wbs.preTaskAction.key006"]."</font>";
}else {
	if ($sAction == ACTION_INSERT."CC") {
		//构造数组进行前置任务校验，只有在添加新前置任务时才需要做此校验
		//先获取项目ID
		$iProjectID = $DB->get_firstfield("Select ProjectID from $TABLE_WBS where ID=$iTaskID");
	
		//获取该项目前置任务列表
		$aPretask = array(array());
		$sql  = "Select a.TaskID, b.OrderNumb From $TABLE_PRETASKLIST a,$TABLE_WBS b ";
		$sql .= "Where b.ProjectID=$iProjectID and a.PreTaskID=b.ID ";
		$sql .= "order by a.TaskID,b.OrderNumb";
		$result = $DB->query($sql);
		$aIndex = 0;
		while ($rs = mysql_fetch_array($result)) {
			$aPretask[$aIndex][0] = $rs["TaskID"];
			$aPretask[$aIndex][1] = $rs["OrderNumb"];
			$aIndex++;
		}
		//将要添加的前置任务也列出来一起检测
		$aPretask[$aIndex][0] = $iTaskID;
		$aPretask[$aIndex][1] = $iOrderNumb;
		mysql_free_result($result);
		//print_r($aPretask);echo "<br>";
	
		//获取所选择项目的工作分解结构列表
		$sql = "Select ID,OrderNumb from $TABLE_WBS where ProjectID=$iProjectID order by OrderNumb";
		$result = $DB->query($sql);
		$aIndex = 1;
		$aTask  = array();
		$bTask  = array();
		while ($row = mysql_fetch_array($result)) {
			$aTask[$aIndex] = $row["OrderNumb"];
			//从$aPretask数组中获取前置任务，并构造新的数组
			$bTask[$aIndex] = array();
			$k = 1;
			for ($i=0; $i<count($aPretask)&&count($aPretask[0])>0; $i++) {
				if ($aPretask[$i][0] == $row["ID"]) {
					$bTask[$aIndex][$k] = $aPretask[$i][1];
					$k++;
				}
			}
			$aIndex++;
		}
		mysql_free_result($result);
		//初始化类，调用检测函数，检测完就摧毁数组变量
		$cCheck = new PreTaskCheck();
		$sResult= $cCheck->startCheck($aTask,$bTask);
		//print_r($aTask);echo "<br>";print_r($bTask);
		unset($aTask);
		unset($bTask);
		unset($aPretask);

		if ($sResult == "" || is_null($sResult)) { //为空，则添加此前置任务
			$sql  = "Insert into $TABLE_PRETASKLIST(TaskID,PreTaskID,PreTaskType,StayTime) ";
			$sql .= "Values($iTaskID,$iPreTaskID,'$sPreTaskType','$sStayTime')";

			$DB->query($sql);
			//$sResult = $messages["apps.wbs.preTaskAction.key007"];   //已经成功添加前置任务
			echo "<script language='javascript'>location.href='wbs.php?strFromWhere=wbslist&strTab=PreTaskTab&&strAction=UPDATE&&intTaskID=".$iTaskID."'</script>";
		} else 
			$sResult = "<font color='red'>".$messages["apps.wbs.preTaskAction.key009"].$sResult."</font>";//前置任务检测有问题：
	}else if ($sAction == ACTION_UPDATE."CC") {
		$sql  = "Update $TABLE_PRETASKLIST Set PreTaskType='$sPreTaskType',StayTime='$sStayTime' ";
		$sql .= "Where ID=$iID";
		$DB->query($sql);
		echo "<script language='javascript'>location.href='wbs.php?strFromWhere=wbslist&strTab=PreTaskTab&&strAction=UPDATE&&intTaskID=".$iTaskID."'</script>";
	}
}

if ($sAction == ACTION_UPDATE) {
	$sql = "Select * from $TABLE_PRETASKLIST Where ID=$iID";
	if ($row = mysql_fetch_array($DB->query($sql))) {
		$iPreTaskID = $row["PreTaskID"];
		$sStayTime  = $row["StayTime"];
	}
}
fn_printHeader();
?>
<script language="javascript">
<!--
function selectTask() {
	var s = ff.strPreTaskID.options(ff.strPreTaskID.selectedIndex).text;
	var numb = s.substr(0,s.indexOf("=="));
	ff.strOrderNumb.value = numb;
}
function checkFrm(f) {
	if (f.strPreTaskID.value=="" || f.strOrderNumb.value=="") {
		alert('<?php echo $messages["apps.wbs.preTask.key001"];?>');
		return false;
	} else
		return true;
}
//-->
</script>
<body  onload="setTableBackColor(PreTaskTable);">
<?php 
echo $messages["navigation"];
echo "<a href='wbslist.php?intProjectID=".$iProjectID."'>".$messages["apps.wbs.wbslist.key009"]."</a>"."&nbsp;>>&nbsp;";
echo "<a href='wbs.php?strFromWhere=wbslist&strTab=PreTaskTab&&strAction=UPDATE&&intTaskID=".$iTaskID."'>".$messages["apps.wbs.wbs.key008"]."</a>"."&nbsp;>>&nbsp;";
echo "<b>";
switch ($sAction) {
	case ACTION_INSERT: echo $messages["apps.wbs.preTask.key002"];break; 
	case ACTION_INSERT."CC": echo $messages["apps.wbs.preTask.key002"];break; 
	case ACTION_UPDATE: echo $messages["apps.wbs.preTask.key003"];break;
	case ACTION_UPDATE."CC": echo $messages["apps.wbs.preTask.key003"];break;
} 
echo "</b>";

if($sAction == ACTION_INSERT."CC" )
{
	echo "<br><tr align='left'><td><font color='red'>".$sResult."</font></td></tr>";
}
?>

<form name="ff" action="preTask.php" method="post">
<input type="hidden" name="strAction"    value="<?php echo $sAction."CC";?>">
<input type="hidden" name="intTaskID"    value="<?php echo $iTaskID;?>">
<input type="hidden" name="intID"        value="<?php echo $iID;?>">

<table border=0 width="100%" align="center" id="PreTaskTable">
<tr>
	<td height='25'><?php echo $messages["apps.wbs.preTask.key014"];?></td>
	<td>
<?php
$sql       = "select TaskName from $TABLE_WBS where ID=".$iTaskID;
$sTaskName = $DB->get_firstfield($sql); 
echo $sTaskName; 
?>
	</td></tr>
<tr>
	<td width="20%"><?php echo $messages["apps.wbs.preTask.key004"];?></td>
	<td><Select name="strPreTaskID" size="1" onchange="selectTask();" 
		<?php if ($sAction==ACTION_UPDATE) echo "disabled";?>>
		<option value=""> <?php echo $messages["apps.wbs.preTask.key005"];?> </option>
	<?php
	$sql  = "Select ID,OrderNumb,TaskName From $TABLE_WBS ";
	$sql .= "Where EPID=$iEPID and ProjectID=$iProjectID and ID<>$iTaskID Order By OrderNumb";
	$ret = $DB->query($sql);
	while ($row=mysql_fetch_array($ret)) {
		echo "\t<option value=\"".$row["ID"].($row["ID"]==$iPreTaskID?"\" Selected>":"\">");
		echo $row["OrderNumb"]."==".$row["TaskName"]."</option>\n";
		if ($row["ID"]==$iPreTaskID) $iOrderNumb=$row["OrderNumb"];
	} ?>
		</Select>
	</td>
</tr>
<tr>
	<td><?php echo $messages["apps.wbs.preTask.key006"];?></td>
	<td><input type="text" name="strOrderNumb" value="<?php echo $iOrderNumb;?>" size="12" readonly></td>
</tr>
<tr>
	<td><?php echo $messages["apps.wbs.preTask.key007"];?></td>
	<td><Select name="strPreTaskType" size="1">
			<option selected value="FS"><?php echo $messages["apps.wbs.preTask.key008"];?></option>
		</Select>
	</td>
</tr>
<tr>
	<td><?php echo $messages["apps.wbs.preTask.key009"];?></td>
	<td><input type="text" name="strStayTime" value="<?php echo $sStayTime;?>" size="12"><?php echo $messages["apps.wbs.preTask.key010"];?></td>
</tr>
</table>
</form>
<hr border="1" style="color:buttonface;width:95%;">
<div align="center">
<input class="button" type="button" name="btnOK" onclick="if (checkFrm(ff)) ff.submit();" value='<?php echo $messages["apps.wbs.preTask.key011"];?>'>
	 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<?php
echo "<input class='button' type='button' name='btnReturn' onclick=\"location.href='wbs.php?strFromWhere=wbslist&strTab=PreTaskTab&&strAction=UPDATE&&intTaskID=".$iTaskID."'\" value='".$messages["apps.wbs.preTask.key013"]."'>";
?> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</div>


<?php fn_printFooter();?>
